#include "layer_type.h"
#include <QColor>

namespace qbe::utils::TempRender {
std::map<std::string, FillParm> fill_types = {
    // {"F0", {-1, 0, 0, 0, 0, 0, 0, 1}},      // 填充
    // {"F1", {2, 15, 0, 0, 45, 0, 0, 1}},     // 斜线1
    // {"F2", {5, 15, 0, 0, 45, 0, 0, 1}},     // 斜线2
    // {"F3", {2, 15, 0, 0, 90, 0, 0, 1}},     // 斜线3
    // {"F4", {1, 20, 0, 0, 45, 1, 0, 1}},     // 斜网格
    // {"F5", {2, 20, 5, 10, 45, 0, 0, 1}},    // 间隙线
    // {"F6", {5, 15, 10, 5, 45, 0, 0, 1}},    // 循环方格点
    // {"F7", {2, 6, 0, 0, 45, 1, 1, 1}},      // 点阵密集
    // {"F8", {15, 25, 0, 0, 45, 1, 1, 1}},    // 点阵
    // {"F9", {1, 20, 10, 10, 45, 1, 0, 1}},   // 循环十字
    // {"F10", {2, 40, 20, 20, 0, 1, 0, 2}},   // 循环十字2
    // {"F11", {6, 40, 10, 10, 45, 1, 0, 2}},  // 特殊纹理1
    // {"F12", {5, 20, 40, 10, 45, 1, 0, 2}},  // 特殊纹理2
    // {"F13", {2, 10, 20, 10, 45, 1, 0, 1}},  // 特殊纹理3
    // {"F14", {4, 20, 20, 20, 45, 1, 0, 1}},  // 特殊纹理4

    {"F0", {0, 0, 0}},  // 全填充
    {"F1", {1, 0, 0}},  // 关闭填充

    {"F2", {2, 1, 5}},   // 点阵1
    {"F3", {2, 1, 10}},  // 点阵2
    {"F4", {2, 2, 5}},   // 点阵3
    {"F5", {2, 2, 10}},  // 点阵4

    {"F6", {3, 1, 5}},   // 右斜线1
    {"F7", {3, 1, 10}},  // 右斜线2
    {"F8", {3, 2, 5}},   // 右斜线3
    {"F9", {3, 2, 10}},  // 右斜线4

    {"F10", {4, 1, 5}},   // 左斜线1
    {"F11", {4, 1, 10}},  // 左斜线2
    {"F12", {4, 2, 5}},   // 左斜线3
    {"F13", {4, 2, 10}},  // 左斜线4

    {"F14", {5, 1, 5}},   // 斜线网格1
    {"F15", {5, 1, 10}},  // 斜线网格2
    {"F16", {5, 2, 5}},   // 斜线网格3
    {"F17", {5, 2, 10}},  // 斜线网格4

    {"F99", {-1, 2, 10}},  // 特殊纹理4
};

std::map<std::string, BorderParm> border_types = {
    {"B1", {0, 0}},     //
    {"B2", {100, 70}},  //
    {"B3", {50, 50}},   //
    {"B4", {20, 20}},   //
    {"B5", {10, 10}},   //
    {"B99", {1.5, 3}},  //
};
LayerType::LayerType() {
  vsg::vec4 border_color = {1.0, 1.0, 0.5, 1.0};
  addLayerParam(0, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F0")->second, border_color, border_types.find("B1")->second);
  addLayerParam(1, vsg::vec4(0.0, 1.0, 0.0, 1.0), fill_types.find("F1")->second, border_color, border_types.find("B1")->second);
  addLayerParam(2, vsg::vec4(0.0, 0.0, 1.0, 1.0), fill_types.find("F2")->second, border_color, border_types.find("B2")->second);
  addLayerParam(3, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F3")->second, border_color, border_types.find("B2")->second);
  addLayerParam(4, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F4")->second, border_color, border_types.find("B2")->second);
  addLayerParam(5, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F5")->second, border_color, border_types.find("B2")->second);
  addLayerParam(6, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F6")->second, border_color, border_types.find("B3")->second);
  addLayerParam(7, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F7")->second, border_color, border_types.find("B3")->second);
  addLayerParam(8, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F8")->second, border_color, border_types.find("B4")->second);
  addLayerParam(9, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F9")->second, border_color, border_types.find("B3")->second);
  addLayerParam(11, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F10")->second, border_color, border_types.find("B3")->second);
  addLayerParam(12, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F11")->second, border_color, border_types.find("B1")->second);
  addLayerParam(13, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F12")->second, border_color, border_types.find("B3")->second);
  addLayerParam(14, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F13")->second, border_color, border_types.find("B2")->second);
  addLayerParam(15, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F14")->second, border_color, border_types.find("B3")->second);
  addLayerParam(16, vsg::vec4(1.0, 0.0, 0.0, 1.0), fill_types.find("F14")->second, border_color, border_types.find("B5")->second);
}

LayerType::~LayerType() {}

void LayerType::addLayerParam(int number, vsg::vec4 fill_color, FillParm fill_parm, vsg::vec4 border_color, BorderParm border_parm) {
  m_layer_params.insert({number, {fill_color, fill_parm, border_color, border_parm}});
}

bool LayerType::findLayerParam(int number, LayerParam& param) {
  auto type = m_layer_params.find(number);
  if (type != m_layer_params.end()) {
    param = type->second;
    return true;
  }
  return false;
}

void getLayerParam(std::vector<std::string> layer_info, LayerParam& layer_param) {
  std::string fill_color = layer_info[0];
  std::string line_color = layer_info[1];
  std::string fill_stype = layer_info[2];
  std::string line_stype = layer_info[3];
  auto colorToRGB = [](const std::string& fill_color) {
    QColor qc(fill_color.c_str());
    int r, g, b;
    qc.getRgb(&r, &g, &b);
    return vsg::vec4(r / 255.0, g / 255.0, b / 255.0, 1.0);
  };

  if (fill_stype == "F99" || fill_stype == "") {
    layer_param.fill_color = vsg::vec4(0.0, 0.0, 0.0, 0.0);
    layer_param.fill_parm = fill_types.find("F0")->second;
  } else {
    layer_param.fill_color = colorToRGB(fill_color);
    layer_param.fill_parm = fill_types.find(fill_stype)->second;
  }
  if (line_stype == "B99" || fill_stype == "") {
    layer_param.border_color = vsg::vec4(0.0, 0.0, 0.0, 0.0);
    layer_param.border_parm = border_types.find("B1")->second;
  } else {
    layer_param.border_color = colorToRGB(line_color);
    layer_param.border_parm = border_types.find(line_stype)->second;
  }
}
BorderParm getLineType(std::string info) {
  BorderParm border_parm;

  if (info == "B99" || info == "") {
    border_parm = border_types.find("B1")->second;
  } else {
    border_parm = border_types.find(info)->second;
  }
  return border_parm;
}
}  // namespace qbe::utils::TempRender